package com.ruoyi.web.controller.business;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TbInspectorLog;
import com.ruoyi.system.domain.TbSearchlightQueue;
import com.ruoyi.system.domain.dto.SearchlightQueueDto;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ITbSearchlightService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 质检Controller
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/business/inspector")
public class TbInspectorController extends BaseController
{
    @Autowired
    private ITbSearchlightService searchlightService;

    @Autowired
    private  ISysDeptService sysDeptService;

    /**
     * 查询探照室排队列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SearchlightQueueDto searchlightQueue)
    {
        startPage();

        Long deptId = SecurityUtils.getDeptId();

        SysDept dept = new SysDept();
        dept.setParentId(deptId);
        List<SysDept> deptList = sysDeptService.selectDeptList(dept);
        List<Long> depts = new ArrayList<>();
        if (!deptList.isEmpty()) {
            for (SysDept item : deptList) {
                depts.add(item.getDeptId());
            }
        } else {
            depts.add(deptId);
        }
        searchlightQueue.setDeptIds(depts.toArray(new Long[depts.size()]));

        List<TbSearchlightQueue> list = searchlightService.selectTbSearchlightQueueList(searchlightQueue);
        return getDataTable(list);
    }

    /**
     * 查询探照室排队列表
     */
    @GetMapping(value = "/list/{deptId}")
    public AjaxResult getSearchlightQueueList(@PathVariable("deptId") Long deptId)
    {
        SearchlightQueueDto dto = new SearchlightQueueDto();
        dto.setDeptId(deptId);
        dto.setPreDate(LocalDate.now().toString());
        dto.setStatus("-1");
        return success(searchlightService.selectTbSearchlightQueueList(dto));
    }

    /**
     * 获取探照室排队详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(searchlightService.selectTbSearchlightQueueById(id));
    }

    /**
     * 质检员操作日志
     */
    @PostMapping("/log")
    public AjaxResult log(@RequestBody TbInspectorLog inspectorLog)
    {
        return toAjax(searchlightService.inspectorLog(inspectorLog));
    }

    /**
     * 质检员检测
     */
    @PostMapping("/inspect")
    public AjaxResult inspect(@RequestBody TbSearchlightQueue searchlightQueue)
    {
        return toAjax(searchlightService.searchlightInspect(searchlightQueue));
    }

    /**
     * 质检员修改探照室排队
     */
    @PutMapping
    public AjaxResult edit(@RequestBody TbSearchlightQueue searchlightQueue)
    {
        return toAjax(searchlightService.updateTbSearchlightQueueByInspector(searchlightQueue));
    }
}
